package com.gsi.webstore.platform.storefront.pipelet.orderprocess;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.intershop.beehive.bts.capi.orderprocess.LineItem;
import com.intershop.beehive.bts.capi.orderprocess.ProductLineItem;
import com.intershop.beehive.bts.capi.orderprocess.basket.Basket;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;

/**
 * 
 */
public class CheckProductCountInPLI extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'Cart'
     */
    public static final String DN_CART = "Cart";
    /**
     * Constant used to access the pipeline dictionary with key 'ERRORMAP'
     */
    public static final String DN_ERRORMAP = "ERRORMAP";
    /**
     * Constant used to access the pipeline dictionary with key
     * 'ErrorMapIterator'
     */
    public static final String DN_ERROR_MAP_ITERATOR = "ErrorMapIterator";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict) throws PipeletExecutionException
    {

        String ERROR_COUNT = "ERROR_PRODUCT_COUNT";
        // lookup 'Cart' in pipeline dictionary
        Basket cart = (Basket)dict.get(DN_CART);
        if (null == cart)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'Cart' not available in pipeline dictionary.");

        }
        Iterator<LineItem> lineItemsIterator = cart.createProductLineItemsOrderedByPositionIterator();
        String firsProductSKU = null;
        ProductLineItem firstLineItem = null;
        Map<String, Integer> productCounterMap = new HashMap<String, Integer>();
        Map<String, String> ErrorMap = new HashMap<String, String>();

        while(lineItemsIterator != null && lineItemsIterator.hasNext())
        {
            firstLineItem = (ProductLineItem)lineItemsIterator.next();
            firsProductSKU = firstLineItem.getOfferProductRef().getProductSKU();

            if (productCounterMap.containsKey(firsProductSKU))
            {
                int tempCount = (Integer)productCounterMap.get(firsProductSKU).intValue();
                tempCount = firstLineItem.getQuantity().getValue().intValue() + tempCount;
                productCounterMap.put(firsProductSKU, new Integer(tempCount));
            }
            else
            {
                productCounterMap.put(firsProductSKU, new Integer(firstLineItem.getQuantity().getValue().intValue()));
            }

        }

        for(Map.Entry e : productCounterMap.entrySet())
        {
            int productCount = (Integer)e.getValue();
            if (productCount > 6)
            {
                String prodSKU = (String)e.getKey();
                ErrorMap.put(prodSKU, ERROR_COUNT);
            }
        }

        // store 'ERRORMAP' in pipeline dictionary
        dict.put(DN_ERRORMAP, ErrorMap);
        Iterator<String> ErrorMapIterator = ErrorMap.keySet().iterator();
        // store 'ErrorMapIterator' in pipeline dictionary
        // TODO Put the value in dictionary
        dict.put(DN_ERROR_MAP_ITERATOR, ErrorMapIterator);
        return PIPELET_NEXT;
    }
}